package com.sundog.sundroid;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import android.widget.Spinner;

import android.view.View.OnClickListener;

/**
 * Class for choosing a resolved vote 
 * 
 * @author Matt
 *
 */
public class Resolve extends Activity implements OnClickListener {
	
	/** Represents this class instance */
	Resolve screen = this;
	/** Resolved vote */
	public static String vote;
	/** On screen spinner */
	Spinner localSpinner;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resolve);
		
		// Set up button click listeners
		View startButton = this.findViewById(R.id.resolve_button);
        startButton.setOnClickListener(this);
        View cancelButton = this.findViewById(R.id.cancel_button);
        cancelButton.setOnClickListener(this);
        
        // Initialize variables
		localSpinner = (Spinner)findViewById(R.id.color_spinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.color_array, R.layout.spinner);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		localSpinner.setAdapter(adapter);    
	}
	
	/**
	 * Process button clicks
	 */
	public void onClick(View v) {
    	switch (v.getId()) {
    	case R.id.resolve_button:
    		vote = (String) localSpinner.getSelectedItem();
    		Log.d("***", "vote feteched  " + vote);
    		
    		confirmationAlert(vote);
    		break;
    	case R.id.cancel_button:
    		screen.finish();
    		break;
    	}
	}

	/**
	 * Confirm vote selection
	 * 
	 * @param vote
	 * 					the vote to confirm
	 */
	private void confirmationAlert(final String vote) {
		new AlertDialog.Builder(this)
        .setTitle("Are you sure")
        .setMessage(vote)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {	                	
                	Intent i = new Intent(screen, ResolveVote.class);
                	i.putExtra("resolvedVote", vote);
            		startActivity(i);
                }
        }) 
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            }
        }).show();
	}  
}